Skip to content

fix(agc): do not cache page states of host regions in the guest arena - #1039

Open
Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/24216
Open

Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/24216

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#24216, originally opened by XFlawless1.

Source commit: a3a71daf879145675fc18a7475f5e3e293f2505d. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

agc_driver_image_table_cache fails on some Windows machines with "a result without Unmapped entries is not cacheable" (boykopovar#24186). The cause is the page-state cache in GuestMemory.cpp, not the compiler.

The cache records the access of every page it queries inside the guest arena range, and relies on libc to report mapping and protection changes so it can forget them. Host-owned regions inside that range (memory already in use when the arena was reserved at load) change without libc knowing, so their cached state goes stale. A page committed after a query stays inaccessible, and a page that is decommitted or made no-access stays readable.

In the report, the test's VirtualAlloc(nullptr, ...) returned 0x340000, which is a host region of the arena (diagnostics). The first capture read the committed first page, which gave its cache chunk an entry, and recorded the reserved second page as inaccessible. The test then committed that page with VirtualAlloc, and the second capture still found it Unmapped. Where the test lands above the arena (0xfc00000000 here), the pages are not cached and the test passes, which is why it reproduces on some machines and not others.

  • PageStates::record no longer stores pages that overlap a host region in the arena span (GuestArenaHostRegionOverlaps_nid_postfix). Those pages are queried every time.
  • PageStates::spanOf looks up the image span before the arena span. The main image is a host region when it loads inside the arena range (the test executables' preferred base is 0x140000000), and it keeps its cache through the image span, which registry mutations invalidate as before.
  • agc_page_state_host_region (new mode of agc_page_state_tests): takes two pages of a process-heap block, which is a host region of the arena. It decommits the second, reads the first, checks that the second is inaccessible, commits it again and checks that it is accessible. Then it makes the first no-access and checks that it is inaccessible. It returns 77 (skipped) when the process heap is not in a host region.

Tested

Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D, Release, Ninja.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: agc_driver_image_table_cache fails on Windows (GCC 16.2.0): "a result without Unmapped entries is not cacheable" boykopovar/AnyPS5#24186
  • Unblocks: none
  • GPUs tested: n/a (CPU-side page-state cache; no GPU work in the tests)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory page-state tracking when image and arena regions overlap, with image regions taking precedence.
    • On Windows, overlapping guest-arena host regions are no longer recorded as arena ranges.

XFlawless1 and others added 2 commits November 20, 2026 16:03
The page-state cache records the access of every page it queries in the
guest arena range, and relies on libc reporting mapping and protection
changes to forget them. Host-owned regions inside that range (memory in
use when the arena was reserved) change without libc knowing, so their
cached state went stale: a page committed after a query stayed
inaccessible, and a page decommitted or made no-access stayed readable.

On a machine where the test's VirtualAlloc landed in such a region,
agc_driver_image_table_cache found its newly committed records still
Unmapped (boykopovar#24186).

Pages that overlap a host region are no longer recorded in the arena
span, and the image span is looked up before the arena span so the main
image keeps its cache when it lies inside the arena range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The arena host region query is declared only on Windows, so the unconditional call broke the Linux build.
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Guest memory page states

Layer / File(s) Summary
Page-state overlap handling
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PageStates::spanOf checks the image span before the arena span. On Windows, PageStates::record skips arena ranges that overlap a guest-arena host region.
Host-region test
core/libs/prx/libSceAgcDriver/tests/PageStates.cpp, core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake
The test checks page states after decommit, recommit, and protection changes. It returns 77 when the fixture does not overlap a host region. CMake registers the test and treats return code 77 as a skip.

Suggested reviewers: milescoviello

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · up to a3a71

This change stops stale page-state cache entries for host-owned regions in the guest arena and adds a Windows test for them. No concrete merge-blocking risk was found. The new test may be skipped on machines where its fixture does not land in a host region.

Security architecture risk: Minimal · up to a3a71

AgcDriver now checks current Windows page protections for host-owned ranges that do not use the main-image cache, rather than reusing arena cache entries that libc cannot reliably invalidate. The change does not grant new memory permissions, and the reviewed paths show no material increase in exposure.

Security review details

Security Blast Radius

  • observed — The changed decision affects in-process accessibility and committed-range classification used by AgcDriver reads and write-back. The arena recording exclusion is Windows-only; image precedence also applies to Linux when the image cache is available.

Trust Boundaries and Controls

  • observed — A supplied address reaches a lookup, not a permission change. Windows GuestArena’s startup classification controls whether the arena can retain the query result, while the live query uses Windows commitment and protection information to classify access.

Resilience and Maintainability Implications

  • observed — The existing Windows committed-read fallback handles failed reads by forgetting cached state, re-querying protections page by page, and zero-filling unreadable portions on that path. The PR leaves this recovery in place.

Rollout and rollback

  • inferred — No operator action or cache migration is required. The cache is process-local: the new binary changes when Windows protection is consulted, not who sets protection. Restarting with the base binary starts with an empty cache but restores the old behavior of retaining host-region arena entries.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 3
  • Waived: $0.75 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 16a2332e-c94d-4428-8b42-70fc20b4ac7c
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and a3a71da.

Files selected for processing: 3

  • core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
  • core/libs/prx/libSceAgcDriver/tests/PageStates.cpp
  • core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agc_driver_image_table_cache fails on Windows (GCC 16.2.0): "a result without Unmapped entries is not cacheable"

3 participants